package com.lxr0860.lxr0860production_test.itemactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.lxr0860.lxr0860production_test.R;
import com.lxr0860.lxr0860production_test.utils.Contants;
import com.sdk.devicemanager.ICcon;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class LEDActivity extends Activity{
    private Button mBtnScan;
    private TextView mTvScanTip;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Contants.DIALOG_POSITIVE:
                    Intent mIntent=new Intent();
                    Bundle bundle=new Bundle();
                    bundle.putBoolean("result",true);
                    mIntent.putExtras(bundle);
                    LEDActivity.this.setResult(Contants.TEST_ITEM_LED,mIntent);
                    finish();
                    break;
                case Contants.DIALOG_NEGATIVE:
                    finish();
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_led);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        mBtnScan = (Button) findViewById(R.id.btn_scan_start);
        mTvScanTip = (TextView) findViewById(R.id.tv_scan_tip);
    }

    private void initData() {

    }

    private void initEvent() {
        mBtnScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mBtnScan.setEnabled(false);
                mTvScanTip.setText("正在测试");
                new LEDThread().start();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    private class LEDThread extends Thread {
        @Override
        public void run() {
            writeToCtrlFile("/sys/class/leds/green/brightness", "255");
            try {
                Thread.sleep(1000);
                writeToCtrlFile("/sys/class/leds/green/brightness", "0");
                writeToCtrlFile("/sys/class/leds/red/brightness", "255");
                Thread.sleep(1000);
                writeToCtrlFile("/sys/class/leds/red/brightness", "0");
                Thread.sleep(1000);
              ICcon.getInstance().set_LEDStatus(1);
                Thread.sleep(1000);
               ICcon.getInstance().set_LEDStatus(2);
                Thread.sleep(1000);
                ICcon.getInstance().set_LEDStatus(0);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mTvScanTip.setText("测试成功");
                        Contants.showDailog(LEDActivity.this,"测试成功，是否保存",handler, Gravity.TOP);
                    }
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void writeToCtrlFile(String path, String data) {
        try {
            FileOutputStream fps = new FileOutputStream(new File(path));
            fps.write(data.getBytes());
            fps.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}